import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common'
import { ArticelService } from './articel.service'
import { CreateArticelDto } from './dto/create-articel.dto'
import { UpdateArticelDto } from './dto/update-articel.dto'
import { AuthJWT } from 'src/decorator/auth.decorator'
import { GetUserInfo } from 'src/decorator/getUserInfo.decotator'
import { User } from '@prisma/client'
import { Roles } from 'src/guards/roles.decorator'
import { ArticelPolicy } from './articel.policy'
import { RolesGuard } from 'src/guards/roles.guard'
import { SkipThrottle } from '@nestjs/throttler'

@SkipThrottle()
@Controller('articel')
export class ArticelController {
   constructor(private readonly articelService: ArticelService) {}

   @Post()
   @AuthJWT()
   create(@Body() createArticelDto: CreateArticelDto, @GetUserInfo() user: User) {
      return this.articelService.create(createArticelDto, user)
   }

   @Get('/getAll/:categoryId/:page/:userId')
   findAll(@Param('categoryId') categoryId: number, @Param('page') page: number, @Param('userId') userId: number) {
      return this.articelService.findAll(categoryId, page, userId)
   }

   @Get(':id')
   findOne(@Param('id') id: string) {
      return this.articelService.findOne(+id)
   }

   @Get('/bytitle/:title')
   findByTitle(@Param('title') title: string) {
      return this.articelService.findByTitle(title)
   }

   @AuthJWT()
   @Patch(':id')
   update(@Param('id') id: string, @Body() updateArticelDto: UpdateArticelDto) {
      return this.articelService.update(+id, updateArticelDto)
   }

   @Roles(ArticelPolicy, 'articel')
   @UseGuards(RolesGuard)
   @AuthJWT()
   @Delete(':id')
   removeArticle(@Param('id') id: string) {
      return this.articelService.remove(+id)
   }
}
